#include <stdio.h>
#include "forecasting.h"

double des(int time_increment, double past_forecast, double* s_1, double* s_2) {
  (*s_1) = ALPHA * past_forecast + (1 - ALPHA) * (*s_1);
  (*s_2) = ALPHA * (*s_1) + (1 - ALPHA) * (*s_2);

  //printf("\nS1: %f, S2: %f, past: %f\n", *s_1, *s_2, past_forecast);

  double level = (2 + (ALPHA * time_increment)/(1 - ALPHA)) * (*s_1);
  double trend = (1 + (ALPHA * time_increment)/(1 - ALPHA)) * (*s_2);

  return level - trend;
}

double forecast_demand(int period_demand) {
  int k;
  double demand = 0.0;
  double forecast = (double) period_demand;
  double s_1 = forecast;
  double s_2 = forecast / DELTA;

  //printf("\nforecast: %f, demand: %f\n", forecast, period_demand);

  for (k = 0; k < DELTA; k++) {
    forecast = des(k, forecast, &s_1, &s_2);
    demand += forecast;
  }

  return demand;
}
